package com.wobuwen.demossm.controller;

import com.wobuwen.demossm.model.User;
import com.wobuwen.demossm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param loginMap 包含用户名和密码的Map
     * @return 登录结果
     */
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Map<String, String> loginMap) {
        String username = loginMap.get("username");
        String password = loginMap.get("password");
        
        Map<String, Object> result = new HashMap<>();
        
        // 验证登录
        boolean isValid = userService.validateLogin(username, password);
        
        if (isValid) {
            User user = userService.findByUsername(username);
            result.put("success", true);
            result.put("message", "你好,登录成功");
            result.put("user", user);
        } else {
            result.put("success", false);
            result.put("message", "用户名或密码错误");
        }
        
        return result;
    }
    
    /**
     * 获取用户信息
     * @param username 用户名
     * @return 用户信息
     */
    @GetMapping("/{username}")
    public Map<String, Object> getUserInfo(@PathVariable String username) {
        Map<String, Object> result = new HashMap<>();
        
        User user = userService.findByUsername(username);
        
        if (user != null) {
            result.put("success", true);
            result.put("user", user);
        } else {
            result.put("success", false);
            result.put("message", "用户不存在");
        }
        
        return result;
    }
    
    /**
     * 添加用户
     * @param user 用户对象
     * @return 添加结果
     */
    // @PostMapping("/add")
    // public Map<String, Object> addUser(@RequestBody User user) {
    //     Map<String, Object> result = new HashMap<>();
        
    //     boolean success = userService.addUser(user);
        
    //     if (success) {
    //         result.put("success", true);
    //         result.put("message", "用户添加成功");
    //     } else {
    //         result.put("success", false);
    //         result.put("message", "用户添加失败，用户名可能已存在");
    //     }
        
    //     return result;
    // }
    @PostMapping("/add")
    public Map<String, Object> addUsers(@RequestBody List<User> users) {
        Map<String, Object> result = new HashMap<>();
        List<String> messages = new ArrayList<>();

        for (User user : users) {
            boolean success = userService.addUser(user);
            if (success) {
                messages.add(user.getUsername() + " 用户添加成功");
            } else {
                messages.add(user.getUsername() + " 用户添加失败，用户名可能已存在");
            }
        }

        result.put("success", true);
        result.put("messages", messages);
        return result;
    }
    
    /**
     * 更新用户
     * @param user 用户对象
     * @return 更新结果
     */
    @PutMapping("/update")
    public Map<String, Object> updateUser(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        
        boolean success = userService.updateUser(user);
        
        if (success) {
            result.put("success", true);
            result.put("message", "用户更新成功");
        } else {
            result.put("success", false);
            result.put("message", "用户更新失败，用户可能不存在");
        }
        
        return result;
    }
    
    /**
     * 删除用户
     * @param username 用户名
     * @return 删除结果
     */
    @DeleteMapping("/delete/{username}")
    public Map<String, Object> deleteUser(@PathVariable String username) {
        Map<String, Object> result = new HashMap<>();
        
        boolean success = userService.deleteUser(username);
        
        if (success) {
            result.put("success", true);
            result.put("message", "用户删除成功");
        } else {
            result.put("success", false);
            result.put("message", "用户删除失败，用户可能不存在");
        }
        
        return result;
    }
}